// *** LICENSE HEADER ***
// Filename: ./affector.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "affector.h"

affector::~affector() {}

gravityAffector::gravityAffector(float newGravity) {
    gravity = newGravity;
}

void gravityAffector::Modify(particle* toAffect, int dt) {
    //Some basic physics here
    toAffect->speedZ -= gravity * dt;
}

flameAffector::flameAffector(float newCenterX, float newCenterY, float newGravity) {
    centerX = newCenterX;
    centerY = newCenterY;
    gravity = newGravity;
}

void flameAffector::Modify(particle* toAffect, int dt) {
    //accelerate the particle towards the center of the affector at all times
    if (toAffect->posX > centerX) {
        toAffect->speedX -= gravity * dt;
    } else
        toAffect->speedX += gravity * dt;
    if (toAffect->posY > centerY) {
        toAffect->speedY -= gravity * dt;
    } else
        toAffect->speedY += gravity * dt;

}

sphereRotationAffector::sphereRotationAffector(float newCenterX, float newCenterY, float newCenterZ, float newOmega, float newRadius){
    centerX = newCenterX; centerY = newCenterY; centerZ = newCenterZ;
    omega = newOmega;
    radius = newRadius;
}

void sphereRotationAffector::Modify(particle* toAffect, int dt){
    //radius from a central pivot is determined by Z coordinate
    //angle is then calculated from the particle age and (for randomization) particle lifetime
    float square = radius*radius - (toAffect->posZ-centerZ)*(toAffect->posZ-centerZ);
    float factor = sqrt(square>0?square:0);
    toAffect->posX = centerX + cos(omega*(toAffect->age + toAffect->life*937)) * factor;
    toAffect->posY = centerY + sin(omega*(toAffect->age + toAffect->life*937)) * factor;
}
